﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using DeJC.BLL.Managers;
using DeJC.DAL.Model;
using TheList.Helper;

namespace TheList.Controllers
{ 
    public class GeneralTypesController : Controller
    {
        //private TheListEntities db = new TheListEntities();

        //
        // GET: /GeneralTypes/

        public ViewResult Index()
        {
            return View(GeneralMiscTypeManager.GetGeneralMiscTypes());
        }

        //
        // GET: /GeneralTypes/Details/5

        public ViewResult Details(int id)
        {
            GeneralMiscType generalmisctype = GeneralMiscTypeManager.GetById(id);
            return View(generalmisctype);
        }

        //
        // GET: /GeneralTypes/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /GeneralTypes/Create

        [HttpPost]
        public ActionResult Create(GeneralMiscType generalmisctype)
        {
            if (ModelState.IsValid)
            {
                //db.GeneralMiscTypes.Add(generalmisctype);
                //db.SaveChanges();
                GeneralMiscTypeManager.AddGeneralMiscType(generalmisctype);
                return RedirectToAction("Index");  
            }

            return View(generalmisctype);
        }
        
        //
        // GET: /GeneralTypes/Edit/5
 
        public ActionResult Edit(int id)
        {
            GeneralMiscType generalmisctype = GeneralMiscTypeManager.GetById(id);
            return View(generalmisctype);
        }

        //
        // POST: /GeneralTypes/Edit/5

        [HttpPost]
        public ActionResult Edit(GeneralMiscType generalmisctype)
        {
            if (ModelState.IsValid)
            {
                //db.Entry(generalmisctype).State = EntityState.Modified;
                //db.SaveChanges();
                GeneralMiscTypeManager.UpdateGeneralMiscType(generalmisctype);
                return RedirectToAction("Index");
            }
            return View(generalmisctype);
        }

        //
        // GET: /GeneralTypes/Delete/5
 
        public ActionResult Delete(int id)
        {
            GeneralMiscType generalmisctype = GeneralMiscTypeManager.GetById(id);
            return View(generalmisctype);
        }

        //
        // POST: /GeneralTypes/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            //GeneralMiscType generalmisctype = GeneralMiscTypeManager.GetById(id);
            //db.GeneralMiscTypes.Remove(generalmisctype);
            //db.SaveChanges();
            GeneralMiscTypeManager.DeleteGeneralMiscType(id);
            return RedirectToAction("Index");
        }

        public ActionResult GtList(int? pId)
        {
            IEnumerable<GeneralMiscType> generalMiscTypes;
            if (pId != null && pId!=0)
            {
                int parentId = Convert.ToInt32(pId);
                GeneralMiscType generalMiscType = GeneralMiscTypeManager.GetById(parentId);
                generalMiscTypes=GeneralMiscTypeManager.GetChildren(parentId);
                ViewBag.Title=generalMiscType.Name;
            }
            else
            {
                generalMiscTypes = GeneralMiscTypeManager.GetParents();
                ViewBag.Title = "Generales";
            }

            return View(generalMiscTypes);
        }

        public ActionResult GtCreate(int? pId)
        {
            ViewBag.GtName = pId==null? "Tipo general":"";
            return View();
        }

        [HttpPost]
        public ActionResult GtCreate(GeneralMiscType generalMiscType) {
            int pId = Convert.ToInt32(Request.Params["pId"]);
            if(ModelState.IsValid)
            {
                generalMiscType.ParentID = pId;
                generalMiscType.CreatedBy = Utility.GetCurrentUserId;
                generalMiscType.CreationDate = DateTime.Now;
                generalMiscType.Enabled = true;
                generalMiscType.Deleted = false;
                GeneralMiscTypeManager.AddGeneralMiscType(generalMiscType);
                return RedirectToAction("GtList", new {pId});
            }
            ViewBag.GtName=pId>0?"Tipo general":"";
            return View();
        }

        public ActionResult GtEdit(int id)
        {
            return View(GeneralMiscTypeManager.GetById(id));
        }

        [HttpPost]
        public ActionResult GtEdit(GeneralMiscType generalMiscType) {

            if(ModelState.IsValid)
            {
                GeneralMiscTypeManager.UpdateGeneralMiscType(generalMiscType);
                return RedirectToAction("GtList", new {generalMiscType.ParentID});
            }

            return View(generalMiscType);
        }


        //protected override void Dispose(bool disposing)
        //{
        //    db.Dispose();
        //    base.Dispose(disposing);
        //}
    }
}